var carousel = layui.carousel;
$(function(){
    document.onkeydown=function(ev){
        var event=ev ||event
        if(event.keyCode==13){
            login();
        }
    }
})

var bannerList = carousel.render({
    elem: '#test1'
    ,width: '100%' //设置容器宽度
    ,height: '100%'
    ,arrow: 'always' //始终显示箭头
});

/**
 * 登录
 */
function login() {
    var username = $("#username").val();
    var password = $("#password").val();
    $.post("/sys/user/loginVali", {username: username, pwd: password}, function(result) {
        if (200 == result.code) {
            window.location = "/sys/user/index";
        } else {
            alert(result.msg);
        }
    });
}

function getdates() {
    var w_array = new Array("星期天", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六");
    var d = new Date();
    var year = d.getFullYear();
    var month = d.getMonth() + 1;
    var day = d.getDate();
    var week = d.getDay();
    var h = d.getHours();
    var mins = d.getMinutes();
    var s = d.getSeconds();

    if (month < 10) month = "0" + month;
    if (day < 10) day = "0" + day;
    if (h < 10) h = "0" + h;
    if (mins < 10) mins = "0" + mins;
    if (s < 10) s = "0" + s;

    //var shows = '今天是： <span>' + year + '年' + month + '月' + day + '日   '+w_array[week]  +'    '+h + ':' + mins + ':' + s +'</span>';
    var shows = '<span style="color: #333333;font-size: 16px;">今天是： ' + year + '年' + month + '月' + day + '日</span>'+
        '<span style="color: #333333;font-size: 16px;margin: 0 20px;">' + w_array[week] +'</span>' +'<span style="color: #333333;font-size: 16px;">'+h + ':' + mins + ':' + s +'</span>';
    document.getElementById("now_date").innerHTML = shows;
    setTimeout("getdates()",1000);
}

function getBannerImages(){
    $.post('/banner/get_list', function(result) {
        if (200 == result.code) {
            var bannerImages = result.data;
            var html = "";
            $.each(bannerImages, function (index, val) {  // 这里的函数参数是键值对的形式，k代表键名，v代表值
                if(index > 4){
                    return;
                }
                html += '<div><div>' +
                    '<a href="' + val.linkUrl + '" class="dynamic_banner_outer" target="_blank">' +
                    '<img src="' + val.imageUrl + '" />' +
                    '<span class="dynamic_banner_cover">'+val.title+'</span>' +
                    '</a>' +
                    '</div></div>';
            });
            $("#banner_image_list").append(html);
            bannerList.reload({
                elem: '#banner_images'
                ,width: '100%' //设置容器宽度
                ,height: '100%'
                ,arrow: 'always' //始终显示箭头
            });
        }
    });
}

getdates();
getBannerImages();


/**
 * 登出
 */
function logout() {
    $.get("/sys/user/logout?time=" + new Date().getTime(), function (result) {
        if (200 == result.code) {
            window.location = "/api/user/index";
        }
    });
}
